﻿using System;
using wojilu.Apps.Qycms.Domain;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Users.Domain;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class MenuController : BaseController
    {
        public virtual IAdminLogService<SiteLog> logService { get; set; }

        public MenuController()
        {
            logService = new SiteLogService();
        }

        private void setpublic()
        {
            User user = ctx.viewer.obj as User;
            var modelPowerGroup = PowerGroup.findById(user.GroupId);
            bind("powergroup", modelPowerGroup);
        }

        [HttpGet, HttpPost]
        public virtual void MenuLinkList()
        {
            SetPublic();
            setpublic();
            set("view", to(new QycmsController().Admin_Tab_List));
            Int32 isshow = ctx.GetInt("isshow");
            Int32 mlid = ctx.GetInt("mlid");
            String loadfun = ctx.Get("loadfun");
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            string limitkey = ctx.Get("limitkey");
            string limitclass = ctx.Get("limitclass");
            DataPage<Menulink> list = null;
            var condition = String.Empty;
            if (loadfun == "menulinklist" || mlid > 0)
            {
                switch (isshow)
                {
                    case 0:
                        condition = " topmlid=" + mlid;
                        break;

                    case 1:
                        condition = " topmlid=" + mlid + " and isshow=1";
                        break;

                    case 2:
                        condition = " topmlid=" + mlid + " and isshow=0";
                        break;
                }
            }
            else
            {
                switch (isshow)
                {
                    case 0:
                        condition = " topmlid > 0";
                        break;

                    case 1:
                        condition = " topmlid >" + mlid + " and isshow=1";
                        break;

                    case 2:
                        condition = " topmlid >" + mlid + " and isshow=0";
                        break;
                }
            }
            if (strUtil.IsNullOrEmpty(limitkey)) limitkey = "pid";
            if (strUtil.IsNullOrEmpty(limitclass)) limitclass = "asc";
            condition += " order by " + limitkey + " " + limitclass;
            CurrentRequest.setCurrentPage(page_id);
            list = Menulink.findPage(condition, maxperpage);
            if (ctx.web.postValueAll().Count == 0)
            {
                ctx.web.ResponseWrite(list.RecordCount.ToString());
                ctx.web.ResponseEnd();
            }
            bind("list", list.Results);
        }

        [HttpGet]
        public virtual void MenuLinkUpdate()
        {
            SetPublic();
            setpublic();
            set("IframeName", ctx.Get("IframeName"));
            set("IframeHeightWindow", ctx.Get("IframeHeightWindow"));
            set("load", to(new AdminController().Load) + "?");
            set("tab", "true");
            var Id = ctx.GetLong("Id");
            Menulink model = Menulink.findById(Id);
            bind("menulink", model);
            target(to(Edit, Id));
        }

        [HttpPost]
        public virtual void Edit(long Id)
        {
            Menulink m = Menulink.findById(Id);
            m = ctx.PostValue(m) as Menulink;
            Result result = db.update(m);
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("menulink_edit_log") + lang("log_extra_no"), "{Id:" + Id + ", Name:'" + m.menuname + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("menulink_edit_log") + lang("log_extra_ok"), "{Id:" + Id + ", Name:'" + m.menuname + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void MenuLinkAdd()
        {
            SetPublic();
            setpublic();
            set("IframeName", ctx.Get("IframeName"));
            set("IframeHeightWindow", ctx.Get("IframeHeightWindow"));
            set("load", to(new AdminController().Load) + "?");
            set("tab", "true");
            var Id = ctx.GetLong("mlid");
            set("Id", Id);
            target(to(Add));
        }

        [HttpPost]
        public virtual void Add()
        {
            Menulink m = ctx.PostValue<Menulink>();
            m.type = 0;
            m.pid = 50;
            m.isshow = 1;
            m.classname = String.Empty;
            m.ismenu = 0;
            Result result = db.insert(m);
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("menulink_add_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("menulink_add_log") + lang("log_extra_ok"), "{Id:" + m.Id + ", Name:'" + m.menuname + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpPost, HttpDelete]
        public virtual void Delete()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            if (db.deleteBatch<Menulink>(condition) > 0)
            {
                logService.Add((User)ctx.viewer.obj, lang("menulink_del_log") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("menulink_del_log") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void Sort()
        {
            var infoid = ctx.PostIdList("infoid");
            var pid = ctx.PostIdList("pid");
            var strArray = infoid.Split(new char[] { ',' });
            var strArray1 = pid.Split(new char[] { ',' });
            for (int i = 0; i < strArray.Length; i++)
            {
                string condition = string.Format("Id = {0}", strArray[i]);
                db.updateBatch<Menulink>("set pid=" + strArray1[i], condition);
            }
            logService.Add((User)ctx.viewer.obj, lang("menulink_log_sort") + lang("log_extra_ok"), "{Id:" + infoid + "'}", "", ctx.Ip);
            ctx.web.ResponseWrite("true");
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void ShowHide()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            var value = ctx.Post("value");
            var dbname = ctx.Post("dbname");
            db.updateBatch<Menulink>("set " + dbname + "=" + value, condition);
            if (!ctx.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("menulink_setting_log") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("menulink_setting_log") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

    }
}
